
package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;

public class ZhangdouJPanel extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    boolean tou_tao;//true 逃跑     false 投降
    ZhangDou zhangdou;

    JLabel chakang;
    JLabel teshu;
    JLabel touxiang_taopao;
    JLabel zhangdouxxi[];
    public ZhangdouJPanel(DuobaoSwingLaunch game, ZhangDou zhangdou, boolean b)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);
        this.zhangdou=zhangdou;

        this.tou_tao=b;
        setGongneng();
        setTishi();
        setThrad();

    }
    private void setThrad()
    {
        Thread th = new Thread(this.zhangdou);
        th.start();
    }

    private void setTishi() {

        String ss[] =SplitString.splitString3(zhangdou.getStrs()) ;
        int a = ss.length;

        zhangdouxxi = new JLabel[15];
        for(int i=0;i<15;i++)
        {
            zhangdouxxi[i]=new JLabel();
            zhangdouxxi[i].addMouseListener(this);
            zhangdouxxi[i].setFont(myFont.f2);
            if(i<a)
            {
                zhangdouxxi[i].setText(ss[a>15?a-15+i:i]);
            }else
            {
                zhangdouxxi[i].setText("");
            }

            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(zhangdouxxi[i]);
            this.add(jp1);
        }
    }
private void gengxinTishi() {

        String ss[] =SplitString.splitString3(zhangdou.getStrs()) ;
        int a = ss.length;


        for(int i=0;i<15;i++)
        {
            if(i<a)
            {
                zhangdouxxi[i].setText(ss[a>15?a-15+i:i]);
            }else
            {
                zhangdouxxi[i].setText("");
            }
        }
    }
    private void setGongneng() {
        chakang=new JLabel();
        chakang.addMouseListener(this);
        chakang.setFont(myFont.f2);
        chakang.setForeground(Color.BLUE);
        chakang.setText("<html><u>"+"查看战况"+"</u>"+"&nbsp&nbsp"+"</html>");

        teshu=new JLabel();
        teshu.addMouseListener(this);
        teshu.setFont(myFont.f2);
        teshu.setForeground(Color.BLUE);
        teshu.setText("<html><u>"+"特殊攻击"+"</u>"+"&nbsp&nbsp"+"</html>");

        touxiang_taopao=new JLabel();
        touxiang_taopao.addMouseListener(this);
        touxiang_taopao.setFont(myFont.f2);
        touxiang_taopao.setForeground(Color.BLUE);
        if(this.tou_tao)
        {
            touxiang_taopao.setText("<html><u>"+"逃跑"+"</u></html>");
        }else
        {
            touxiang_taopao.setText("<html><u>"+"投降"+"</u></html>");
        }

        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(chakang);
        jp3.add(teshu);
        jp3.add(touxiang_taopao);
        this.add(jp3);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        for(int i=0;i<this.zhangdouxxi.length;i++)
        {
            if(jl==zhangdouxxi[i])
            {
                f=true;
            }
        }
        return f;
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==this.chakang)
        {
            //this.zhangdou.setTaopao(false);
            if(this.game.hero.getZD()==null)
            {
                this.game.setPanel(new MainPanel(this.game,1));
            }else
            {
                gengxinTishi();
            }

        }else if(e.getSource()==this.teshu)
        {

        }else if(e.getSource()==this.touxiang_taopao)
        {
            if(this.game.hero.getZD()==null)
            {
                this.game.setPanel(new MainPanel(this.game,1));
            }else
            {
                this.zhangdou.setTaopao(true);
                gengxinTishi();
            }
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
